package com.quexin.pickmedialib;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView2;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class PickerMediaAdapter extends BaseQuickAdapter<MediaModel, BaseViewHolder> implements
        OnItemClickListener {

    private final ArrayList<MediaModel> checkModels = new ArrayList<>();
    private final int maxCheckSize;

    public PickerMediaAdapter(@Nullable List<MediaModel> data, int max) {
        super(R.layout.item_picker_media, data);
        setOnItemClickListener(this);
        maxCheckSize = max;
    }

    public PickerMediaAdapter(@Nullable List<MediaModel> data, int max, ArrayList<MediaModel> checkModels) {
        super(R.layout.item_picker_media, data);
        setOnItemClickListener(this);
        maxCheckSize = max;
        this.checkModels.clear();
        this.checkModels.addAll(checkModels);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, MediaModel mediaModel) {
        QMUIRadiusImageView2 imageView2 = baseViewHolder.getView(R.id.qiv2_item);
        if (mediaModel.getFlag() == 1) {
            imageView2.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView2.setImageResource(R.drawable.icon_audio);
        } else {
            imageView2.setScaleType(ImageView.ScaleType.CENTER_CROP);
            Glide.with(getContext()).load(mediaModel.getPath()).into(imageView2);
        }

        CheckBox radioButton = baseViewHolder.getView(R.id.cb_item);
        radioButton.clearFocus();
        radioButton.setChecked(checkModels.contains(mediaModel));

        TextView textView = baseViewHolder.getView(R.id.tv_item);
        if (mediaModel.getFlag() == 0) {
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(mediaModel.getDuration());
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        MediaModel mediaModel = getItem(position);
        if (checkModels.contains(mediaModel)) {
            checkModels.remove(mediaModel);
            notifyItemChanged(position);
        } else {
            if (maxCheckSize == 1 && checkModels.size() == maxCheckSize) {
                int removePosition = getItemPosition(checkModels.get(0));
                checkModels.clear();
                notifyItemChanged(removePosition);
                checkModels.add(mediaModel);
                notifyItemChanged(position);
            } else if (checkModels.size() == maxCheckSize) {
                if (mListener != null) {
                    mListener.maxPickerPrompt();
                }
            } else {
                checkModels.add(mediaModel);
                notifyItemChanged(position);
            }
        }
    }

    public ArrayList<MediaModel> getCheckModels() {
        return checkModels;
    }

    private Listener mListener;

    public void setListener(Listener listener) {
        mListener = listener;
    }

    public interface Listener {

        void maxPickerPrompt();
    }
}
